<?php

namespace App\Http\Resources;

use App\Models\WorkUser;
use App\Models\SchoolUser;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @OA\Schema(
 *     schema="WorkTicketDetailResource",
 *     title="工单详情资源",
 *     description="工单详情信息",
 *     @OA\Property(property="id", type="integer", description="详情ID", example=1),
 *     @OA\Property(property="work_ticket_id", type="integer", description="工单ID", example=1),
 *     @OA\Property(property="related_id", type="integer", description="关联对象ID", example=1),
 *     @OA\Property(property="related_type", type="string", description="关联对象类型", example="App\\Models\\WorkUser"),
 *     @OA\Property(property="content", type="string", description="详情内容", example="已联系用户，约定明天上午维修"),
 *     @OA\Property(property="created_at", type="string", format="date-time", description="创建时间", example="2025-01-01 12:00:00"),
 *     @OA\Property(property="updated_at", type="string", format="date-time", description="更新时间", example="2025-01-01 12:00:00"),
 *     @OA\Property(property="images", type="array", description="关联的图片", @OA\Items(ref="#/components/schemas/WorkTicketImageResource")),
 *     @OA\Property(property="operator_name", type="string", description="操作人名称")
 * )
 * @mixin \App\Models\WorkTicketDetail
 */
class WorkTicketDetailResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $arr =  [
            'id' => $this->id,
            'work_ticket_id' => $this->work_ticket_id,
            'related_id' => $this->related_id,
            'related_type' => $this->related_type,
            'content' => $this->content,
            'created_at' => $this->created_at?->toDateTimeString(),
            'updated_at' => $this->updated_at?->toDateTimeString(),
            'images' => $this->whenLoaded('images', function () {
                return $this->images->pluck('path');
            }),
            'operator_name' => $this->whenLoaded('related', function () {
                if($this->related instanceof SchoolUser){
                    return $this->related->real_name;
                }

                if($this->related instanceof WorkUser){
                    return $this->related->real_name;
                }
                $model = config('admin.database.users_model');
                if($this->related instanceof $model){
                    return $this->related->username;
                }
            })
        ];

        return $arr;
    }
} 